package cn.flying.base.web.auth.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.flying.base.web.auth.domain.SysLoginLog;
import cn.flying.base.web.auth.domain.SysUser;
import cn.flying.base.web.auth.domain.SysUserRole;

/**
 * @description: 用户接口
 * @author: lvyong
 * @date: 2023年05月24日 13:12
 * @version: 1.0
 */
@Mapper
public interface SysUserMapper {

    /**
     * 通过用户名查询用户
     *
     * @param username
     * @return
     */
    @Select("select user_id, username, name, password, org_code, org_name, email, mobile, gender, birthday, address, status, locked, last_login_time, remark, picture, tenant_id from cm_user where username = #{username}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "orgCode", column = "org_code"),
            @Result(property = "orgName", column = "org_name"),
            @Result(property = "email", column = "email"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "address", column = "address"),
            @Result(property = "status", column = "status"),
            @Result(property = "locked", column = "locked"),
            @Result(property = "lastLoginTime", column = "last_login_time"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "picture", column = "picture"),
            @Result(property = "tenantId", column = "tenant_id"),
    })
    SysUser findUserByUsername(String username);

    /**
     * 通过用户ID查询用户
     *
     * @param userId
     * @return
     */
    @Select("select user_id, username, name, password, org_code, org_name, email, mobile, gender, birthday, address, status, locked, last_login_time, remark, picture, tenant_id from cm_user where user_id = #{userId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "name", column = "name"),
            @Result(property = "password", column = "password"),
            @Result(property = "orgCode", column = "org_code"),
            @Result(property = "orgName", column = "org_name"),
            @Result(property = "email", column = "email"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "address", column = "address"),
            @Result(property = "status", column = "status"),
            @Result(property = "locked", column = "locked"),
            @Result(property = "lastLoginTime", column = "last_login_time"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "picture", column = "picture"),
            @Result(property = "tenantId", column = "tenant_id"),
    })
    SysUser findUserByUserId(String userId);

    /**
     * 通过用户ID查询用户角色权限
     *
     * @param userId
     * @return
     */
    @Select("select user_id, role_id, remark, tenant_id from cm_user_role where user_id = #{userId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "roleId", column = "role_id"),
            @Result(property = "remark", column = "remark"),
            @Result(property = "tenantId", column = "tenant_id"),
    })
    List<SysUserRole> getUserRoleList(String userId);

    /**
     * 插入用户
     *
     * @param sysUser
     */
    @Insert({"INSERT INTO cm_user (id, user_id, username, name, password, mobile, status, locked, tenant_id, created_by, created_time, updated_by, updated_time, revision) " +
            "VALUES (#{id}, #{userId}, #{username}, #{name}, #{password}, #{mobile}, #{status}, #{locked}, #{tenantId}, #{createdBy}, #{createdTime}, #{updatedBy}, #{updatedTime}, #{revision})"})
    void insertUser(SysUser sysUser);

    /**
     * 更新用户的最后登录时间
     *
     * @param username
     */
    @Update("update cm_user set last_login_time =now() where username = #{username}")
    void updateUserLoginTimeByUsername(String username);

    /**
     * 更新用户的密码
     *
     * @param sysUser
     */
    @Update("update cm_user set password = #{password}, updated_by = #{updatedBy},updated_time = #{updatedTime} where user_id = #{userId}")
    void updateUserPassword(SysUser sysUser);

    /**
     * 更新用户锁定状态
     *
     * @param sysUser
     */
    @Update("update cm_user set locked = #{locked}, updated_by = #{updatedBy},updated_time = #{updatedTime} where user_id = #{userId}")
    void updateUserLocked(SysUser sysUser);


    /**
     * 插入登录日志
     *
     * @param sysLoginLog
     */
    @Insert({"INSERT INTO cm_login_log (id, user_id, user_name, name, happen_type, happen_time, happen_ip, remark, tenant_id, created_by, created_time, updated_by, updated_time, revision) " +
            "VALUES (#{id}, #{userId}, #{userName}, #{name}, #{happenType}, #{happenTime}, #{happenIp}, #{remark}, #{tenantId}, #{createdBy}, #{createdTime}, #{updatedBy}, #{updatedTime}, #{revision})"})
    void insertLoginLog(SysLoginLog sysLoginLog);

}
